import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;*/

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-10
 * Time: 17:44
 */
public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1：建立数据源，确定数据库的位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("613718long");
        //2：建立连接
        Connection connection = dataSource.getConnection();
        //3：写sql语句并进行预处理
        String sql = "select * from student";
        //4:sql转换为可发送的语句对象
        PreparedStatement statement = connection.prepareStatement(sql);
        //5：执行SQL语句
        ResultSet resultSet = statement.executeQuery(sql);//结果集合
        while(resultSet.next()){//迭代，光标往后走
            //获取id这一列的数值，期望得到一个int返回值
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.print("id:" + id + " ");
            System.out.println("name:" + name + " ");
        }
        //6：释放资源
        resultSet.close();
        statement.close();
        connection.close();


    }
}
/* //1：建立数据源，确定数据库的位置
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java110?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("613718long");
        //2：建立连接
        Connection connection = dataSource.getConnection();
        //3：写sql语句并进行预处理，sql转换为可发送的语句对象
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4：执行SQL语句
        ResultSet resultSet = statement.executeQuery(sql);//结果集合
        while(resultSet.next()){//迭代，光标往后走
            //获取id这一列的数值，期望得到一个int返回值
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.print("id:" + id + " ");
            System.out.println("name:" + name + " ");
        }
        //释放资源
        resultSet.close();
        statement.close();
        connection.close();*/